﻿/* 
 * FreelanceClientControl.cs
 * 
 * Author - Ankit Gupta and Gaurav Aggarwal
 * 
 * This file acts as a controller to Freelance Client tab.
 * 
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Security;
using System.Diagnostics;

partial class FreelanceClientControl : UserControl
{
    // Apply the changes
    private void applyButton_Click(object sender, EventArgs e)
    {
        try
        {   
            RegistryKey serviceParameters = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters", true);
            if (enableFreelanceCheckBox.Checked)
            {
                serviceParameters.SetValue("FreelanceClient", 1, RegistryValueKind.DWord);
            }
            else
            {
                serviceParameters.DeleteValue("FreelanceClient", false);
            }
        }
        catch (SecurityException)
        {
            MessageBox.Show("You do not have access to apply these settings.", "Access Denied", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    // Cancel the operations
    private void cancelButton_Click(object sender, EventArgs e)
    {
        Process[] p = Process.GetProcessesByName("mmc");
        p[0].Kill();
    }

    // Add/Modify freelance objects
    private void buttonAddModify_Click(object sender, EventArgs e)
    {

        if (nameTextBox.Text.Equals(""))
        {
            String error = "Name should be a non-empty string";
            MessageBox.Show(error);
            return;
        }
        else if (targetTextBox.Text.Equals(""))
        {
            String error = "Target should be a non-empty string";
            MessageBox.Show(error);
            return;
        }

        if (symbolicLinkRadioButton.Checked)
        {
            symlinks.SetValue(nameTextBox.Text, targetTextBox.Text, RegistryValueKind.String);
        }

        else if (readOnlyMountPointRadioButton.Checked)
        {
            if (targetTextBox.Text[0]!='.')
            {
                mountpoints.SetValue(nameTextBox.Text, targetTextBox.Text, RegistryValueKind.String);
            }
            else
            {
                String error = "Specified Target cant start with a '.' in case of ReadOnly Mount Point";
                MessageBox.Show(error);
                return;
            }
        }
        else 
        {
            if (targetTextBox.Text[0] == '.')
            {
                {
                    mountpoints.SetValue(nameTextBox.Text, targetTextBox.Text, RegistryValueKind.String);
                }
            }
            else 
            {
                String error = "Specified Target should start with a '.' in case of Read/Write Mount Point";
                MessageBox.Show(error);
                return;
            }        
        }

        // Registry entries of symlinks and mountpoints
        symlinks = Registry.LocalMachine.OpenSubKey("SOFTWARE\\OpenAFS\\Client\\Freelance\\Symlinks", true);
        mountpoints = Registry.LocalMachine.OpenSubKey("SOFTWARE\\OpenAFS\\Client\\Freelance", true);
        
        if (symlinks != null)
        {
            symarr = symlinks.GetValueNames();
            symcount = symlinks.ValueCount;
        }

        if (mountpoints != null)
        {
            mountarr = mountpoints.GetValueNames();
            mountcount = mountpoints.ValueCount;
        } 
        
        datagrid.Rows.Clear();
        for (int i = 0; i < mountcount; i++)
        {
            if (mountpoints.GetValue(mountarr.GetValue(i).ToString()).ToString()[0] == '.')
            {
                datagrid.Rows.Add(mountarr.GetValue(i).ToString(), "Mount Point(RW)", mountpoints.GetValue(mountarr.GetValue(i).ToString()).ToString());
            }
            else
            {
                datagrid.Rows.Add(mountarr.GetValue(i).ToString(), "Mount Point(RO)", mountpoints.GetValue(mountarr.GetValue(i).ToString()).ToString());
            }
        }
        for (int i = 0; i < symcount; i++)
        {
            datagrid.Rows.Add(symarr.GetValue(i).ToString(), "Symbolic Links", symlinks.GetValue(symarr.GetValue(i).ToString()).ToString());
        }
    }

    // Remove freelance object
    private void buttonRemove_Click(object sender, EventArgs e)
    {
        if (nameTextBox.Text.Equals(""))
        {
            String error = "Name should be a non-empty string";
            MessageBox.Show(error);
            return;
        }
        else if(targetTextBox.Text.Equals("")){
            String error = "Target should be a non-empty string";
            MessageBox.Show(error);
            return;
        }

        symlinks = Registry.LocalMachine.OpenSubKey("SOFTWARE\\OpenAFS\\Client\\Freelance\\Symlinks", true);
        mountpoints = Registry.LocalMachine.OpenSubKey("SOFTWARE\\OpenAFS\\Client\\Freelance", true);
        if (symlinks != null)
        {
            symarr = symlinks.GetValueNames();
            symcount = symlinks.ValueCount;
        }


        if (mountpoints != null)
        {
            mountarr = mountpoints.GetValueNames();
            mountcount = mountpoints.ValueCount;
        }
        int index = -1, i = 0;
        if (symbolicLinkRadioButton.Checked)
        {
            for (i = 0; i < symcount; i++) if (nameTextBox.Text.Equals(symarr.GetValue(i).ToString())) index = i;
            if (index != -1)
            {
                if (targetTextBox.Text.Equals(symlinks.GetValue(nameTextBox.Text).ToString()))
                {
                    symlinks.DeleteValue(nameTextBox.Text, false);

                }
            }
        }
        else
        {
            for (i = 0; i < mountcount; i++) if (nameTextBox.Text.Equals(mountarr.GetValue(i).ToString())) index = i;
            if (index != -1)
            {
                if (targetTextBox.Text.Equals(mountpoints.GetValue(nameTextBox.Text).ToString()))
                {
                    mountpoints.DeleteValue(nameTextBox.Text, false);
                }
            }
        }
        symlinks = Registry.LocalMachine.OpenSubKey("SOFTWARE\\OpenAFS\\Client\\Freelance\\Symlinks", true);
        mountpoints = Registry.LocalMachine.OpenSubKey("SOFTWARE\\OpenAFS\\Client\\Freelance", true);
        if (symlinks != null)
        {
            symarr = symlinks.GetValueNames();
            symcount = symlinks.ValueCount;
        }

        if (mountpoints != null)
        {
            mountarr = mountpoints.GetValueNames();
            mountcount = mountpoints.ValueCount;
        }
        datagrid.Rows.Clear();
        for (i = 0; i < mountcount; i++)
        {
            if(mountpoints.GetValue(mountarr.GetValue(i).ToString()).ToString()[0]=='.')
            {
                datagrid.Rows.Add(mountarr.GetValue(i).ToString(), "Mount Point(RW)", mountpoints.GetValue(mountarr.GetValue(i).ToString()).ToString());
            }
            else 
            {
                datagrid.Rows.Add(mountarr.GetValue(i).ToString(), "Mount Point(RO)", mountpoints.GetValue(mountarr.GetValue(i).ToString()).ToString());
            }
        }

        for (i = 0; i < symcount; i++)
        {
            datagrid.Rows.Add(symarr.GetValue(i).ToString(), "Symbolic Links", symlinks.GetValue(symarr.GetValue(i).ToString()).ToString());
        }
    }
}